#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set fileencoding=utf-8
import gootalk
import sys
import pyxmppxmpp as gxmpp
import userman
import logging
import pylock as glock
from pychatuser import ChatUser

HELP_MESSAGE=None

def dealCommand(sender, command):
    logging.info(u'dealCommand(%s)' % (command))
    g = globals()
    cmdName = command.split(' ')[0].lower()
    if (cmdName and cmdName != u'command'):
        dealFuncName = u'deal' + cmdName[:1].upper() + cmdName[1:]
        if (dealFuncName in g):
            g[dealFuncName](sender, command)
            logging.info(u'dealCommand(%s) OK' % (command))
            return gootalk.OK

    logging.info(u'dealCommand(%s) Error' % (command))
    return gootalk.ERROR

def dealHelp(sender, command):
    u'''显示这个坑爹的帮助信息'''
    global HELP_MESSAGE
    if (HELP_MESSAGE == None):
        HELP_MESSAGE=u'\n========帮助=======\n'
        g = globals()
        logging.info(g)
        for cmd in g:
            logging.info(cmd)
            logging.info(g[cmd].__doc__)
            if (cmd[:4] == 'deal' and g[cmd].__doc__):
                HELP_MESSAGE = HELP_MESSAGE + u'#' + cmd[4:].lower() + u' - ' + g[cmd].__doc__ + u'\n'

    gxmpp.sendMessage(sender, HELP_MESSAGE)

def dealKengdie(sender, command):
    u'''这是一个坑爹的命令'''
    u = userman.getUser(sender)
    args = command.split(' ')
    if (len(args) == 1):
        gxmpp.sendMessage(sender, u'你被坑了！')
        if (u):
            gxmpp.sendMessageToOther(u.jid, u'哦也，[' + u.nick + u'] 被坑鸟～～～咩哈哈哈哈！！')
    elif (len(args) == 2):
        tou = userman.getUserByNick(args[1])
        if (tou):
            gxmpp.sendMessage(tou.jid, u'你被不明生物坑了！')
            kdnick = u'不明生物'
            if (u):
                kdnick = u.nick
            gxmpp.sendMessageToOther(tou.jid, u'哦也，[' + tou.nick + u'] 被 [' + kdnick + u']坑鸟～～～咩哈哈哈哈！！')
        else:
            gxmpp.sendMessage(sender, u'坑货，你要坑谁捏！')
    else:
        gxmpp.sendMessage(sender, u'坑货，参数错鸟！')

def dealInvite(sender, command):
    u'''邀请用户，用户邮箱以空格分隔'''
    jid = sender.split('/')[0]
    for u in command.split(' ')[1:]:
        userman.inviteUser(u, jid)

def dealBishi(sender, command):
    jid = sender.split('/')[0];
    args = command.split(' ')
    u = userman.getUser(sender)
    if (len(args) == 3):
        msg = args[1]
        tou = userman.getUserByNick(args[2])
        if (tou):
            gxmpp.sendMessage(tou.jid, u'你被群众鄙视了！')
            gxmpp.sendMessageToOther(tou.jid, u'在各大行星政府的号召下，亿万人民参加了对 [' + tou.nick + u'] 的鄙视活动！' + msg)
        else:
            gxmpp.sendMessage(sender, u'坑货，你要鄙视谁捏！')
    else:
        gxmpp.sendMessage(sender, u'坑货，参数错鸟！')

def dealNick(sender, command):
    u'''修改昵称'''
    logging.info(u'dealNick ' + sender)
    args = command.split(' ')
    if (len(args) > 1):
        nick = sender.split('@')[0]
        u = userman.getUser(sender)
        if (u):
            nick = u.nick
        u = userman.setNick(sender, args[1])
        gxmpp.sendMessageToOther(sender, u'[%s] 现在叫做 [%s] ！' % (nick, u.nick))
        gxmpp.sendMessage(sender, u'你现在叫做 [%s] ！' % (u.nick))
    else:
        gxmpp.sendMessage(sender, u'参数错误哟，亲')
        
def dealList(sender, command):
    u'''列出用户'''
    args = command.split(' ')
    us = userman.getUsers()
    ls = u'\n用户列表\n'
    for u in us:
        ls = ls + u.nick + u'[' + u.jid + u']'
        if (u.available):
            ls = ls + u'(在线)'
        if (u.inviting):
            ls = ls + u' 被 ' + u.inviteby + u' 邀请'
        ls = ls + '\n'
    gxmpp.sendMessage(sender, ls)

def dealM(sender, command):
    u'''发送私信'''
    args = command.split(' ')
    u = userman.addUser(sender)
    if (u and len(args) > 2):
        nick = args[1];
        msg = args[2];
        um = userman.getUserByNick(nick)
        if (um):
            gxmpp.sendMessage(um.jid, u'{' + u.nick + u'} ' + msg)
        else:
            gxmpp.sendMessage(sender, u'没有这个用户哟，亲')

def dealInit(sender, command):
    logging.info('init')
    glock.clear()
    userman.init()

def dealSql(sender, command):
	args = command.split(' ')
	if (len(args) >1):
		ChatUser.sql(args[1])
